package com.example.youyouhaodian.config;

import com.alibaba.druid.support.http.StatViewFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * 开启监测功能：
 *    1.检查和分析数据库运行情况
 *    2.优化数据库的访问性能
 *  步骤：
 *    1.一个监控的服务器
 *    2.过滤器     ----白名单         -----黑名单，
 *      通过访问者的IP地址来控制访问来源，增加数据库的安全设置-http://localhost:8082/druid/index.html
 *
 */
@Configuration
public class DruidConfig {
    //定义一个监控的服务器
    @Bean
    public ServletRegistrationBean statServlet(){
        //检测的访问路径
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
    //Ip白名单
        servletRegistrationBean.addInitParameter("allow","192.168.50.213");
    //IP黑名单(当两个同时存在时，黑名单优先于白名单，deny优先于allow)
 //       servletRegistrationBean.addInitParameter("deny","192.168.50.61");

        //控制台的用户名与密码
        servletRegistrationBean.addInitParameter("loginUsername","druid");
        servletRegistrationBean.addInitParameter("loginPassword","123456");

        //是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    //定义一个过滤器--所有路径都经过过滤器，来监听SQL语句
    public FilterRegistrationBean staatFilter(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new StatViewFilter());
        //添加过滤规则,允许谁进入过滤器
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的格式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.css,*.jpg,*.png,*.jif,/druid/*");
        return filterRegistrationBean;


    }

    /**
     * DruidStatInterceptor类为druid提供的拦截器
     * JdkRegexpMethodPointcut为使用正则表达式配置切点
     * DefaultPointcutAdvisor类定义advice及 pointcut 属性。
     * advice指定使用的通知方式，也就是druid提供的DruidStatInterceptor类，pointcut指定切入点。
     */

    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
        DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
        return dsInterceptor;
    }

    @Bean
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPatterns("com.example.youyouhaodian.dao");

        return pointcut;
    }

    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
        DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointAdvisor;
    }




}
